//
//  SJZSettingController.m
//  SJZMetro
//
//  Created by 彭利民 on 2017/7/5.
//  Copyright © 2017年 bwton. All rights reserved.
//

#import "SJZSettingController.h"
#import "SJZAccountModel.h"
#import "SJZAboutUsController.h"
#import "SJZNavigationController.h"
#import "SJZLoginQuickViewController.h"

@interface SJZSettingController ()

@end

@implementation SJZSettingController

- (void)popself{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    
    self.view.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    self.tableView.rowHeight = 48;

    
//    UIBarButtonItem  *item = [UIBarButtonItem itemWithimage:[UIImage imageNamed:@"return"] highImage:[UIImage imageNamed:@"return"] target:self action:@selector(popself)];
//    
//    self.navigationItem.leftBarButtonItem = item;

    
    UIView *headerView = [[UIView alloc] init];
    headerView.x = 0;
    headerView.y = 0;
    headerView.width = kScreenWidth;
    headerView.height = 150;
    headerView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = headerView;
    
    UIImageView *logoView = [[UIImageView alloc] init];
    logoView.backgroundColor = [UIColor blueColor];
    logoView.image = [UIImage imageNamed:@"logo"];
    logoView.y = 30;
    logoView.width = 75;
    logoView.layer.cornerRadius = 5;
    logoView.clipsToBounds = YES;
    
    logoView.height = logoView.width;
    
    logoView.centerX = headerView.width * 0.5;
    
    [headerView addSubview:logoView];
    
    UILabel *labelView = [[UILabel alloc] init];
    labelView.y = CGRectGetMaxY(logoView.frame) + 10;
    labelView.height = 20;
    labelView.width = kScreenWidth;
    labelView.textAlignment = NSTextAlignmentCenter;
    labelView.text = @"易企乐商家端";
    labelView.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
    
    [headerView addSubview:labelView];

    
    UIView *footerView = [[UIView alloc] init];
    footerView.x = 0;
    footerView.y = 0;
    footerView.width = kScreenWidth;
    footerView.height = 180;
    footerView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    
    self.tableView.tableFooterView = footerView;
    
    
    UIButton *loginBtn = [[UIButton alloc] init];
    
    loginBtn.y = 50;
    loginBtn.width = kScreenWidth - 40;
    loginBtn.centerX = kScreenWidth * 0.5;
    loginBtn.height = 40;
    
    loginBtn.layer.cornerRadius = 6;
    
    [loginBtn setTitle:@"退出" forState:UIControlStateNormal];

    loginBtn.backgroundColor = [UIColor colorWithRed:239/255.0 green:69/255.0 blue:77/255.0 alpha:1.0];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [loginBtn addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    [footerView addSubview:loginBtn];
    
    
    [self removeLogoutBtn];

    
//    self.tableView.separatorStyle =
    
}

- (void)logoutAction{
    
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"退出登录" message:@"真的要退出吗? 退出后将不能使用某些功能噢!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        
        SJZAccountModel *accountModel = [SJZAccountModel userFromArachiver];
        
        accountModel = nil;
        
        BOOL saveSuccess = [SJZAccountModel saveUserObjectWithUser:accountModel];
        
        
#warning 这个地方需要  注意。
        
      

        
        if (saveSuccess) {
            [self removeLogoutBtn];
//            [MBProgressHUD showSuccess:@"用户已退出" toView:self.view];
            [MBProgressHUD showSuccessMessage:@"用户已退出"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kLoginStatusChange object:nil];
        
//        SJZNavigationController *nav = [[SJZNavigationController alloc] initWithRootViewController:[[SJZLoginQuickViewController alloc] init]];
//        [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        

    }
}


-(void)removeLogoutBtn{
    
    SJZAccountModel *accountModel = [SJZAccountModel userFromArachiver];
    
    // 没有token 就 不显示。
    if (!accountModel.token) {
        self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        [self.tableView reloadData];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    label.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    
    return label;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"用户协议";
    }else
    {
        cell.textLabel.text = @"关于我们";
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        
        
    }else if(indexPath.row == 1)
    {
        SJZAboutUsController *protocolVc = [[SJZAboutUsController alloc] init];
        
        [self.navigationController pushViewController:protocolVc animated:YES];

    
    }
}



@end
